﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.IO;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Xsl;
using TanashCMS.Business;

public partial class manager_dialogs_ImageServices : CMSManagementPageBase
{
    private string method,webfolder;
    private Dictionary<string, string> arguments = new Dictionary<string, string>();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["webfolder"] != null)
            webfolder = Request.QueryString["webfolder"];

        if (Request.RequestType.ToLower() == "get")
        {
            if (Request.QueryString["method"] != null)
                method = Request.QueryString["method"];

            foreach (string item in Request.QueryString.AllKeys)
            {
                if (item != "method")
                    arguments.Add(item, Request.QueryString[item]);
            }
        }
        else if (Request.RequestType.ToLower() == "post")
        {
            if (Request.Form["method"] != null)
                method = Request.Form["method"];

            foreach (string item in Request.Form.AllKeys)
            {
                if (item != "method")
                    arguments.Add(item, Request.Form[item]);
            }
        }

        Response.Clear();
        switch (method)
        {
            case "GetImages":
                GetImages();
                break;
        }
    }

    private void GetImages()
    {
        HttpContext context = HttpContext.Current;
        Uri uri = context.Request.Url;

        // find the hostname
        string hostname;
        hostname = uri.Host;
        if (hostname.Substring(0, 4) == "www.")
            hostname = hostname.Substring(4);

        //if (!System.IO.File.Exists(Server.MapPath("~" + mWebsite.ImagesXmlPath)))
        //{
            // update the xml file list
            Web2Xml.writeImagesToXml(mWebsite);
        //}


        StringBuilder sb = new StringBuilder();
        StringWriter w = new StringWriter(sb);

        XslCompiledTransform xsl = new XslCompiledTransform();
        xsl.Load(Server.MapPath("~/manager/dialogs/SelectImage.xsl"));

        XsltArgumentList args = new XsltArgumentList();
        args.AddParam("baseUrl", "", HttpContext.Current.Request.ApplicationPath.TrimEnd('/') + mWebsite.ImagesFolder);
        args.AddParam("imagePath", "", HttpUtility.UrlPathEncode(arguments["path"]));
        args.AddParam("baseImageUrl", "", mWebsite.ImagesFolder);

        xsl.Transform(Server.MapPath("~" + mWebsite.ImagesXmlPath), args, w);

        w.Close();

        Response.Write(sb.ToString());
    }

    protected override void Render(HtmlTextWriter writer)
    {
        //base.Render(writer);
    }
}
